package com.demo.controller;

import com.demo.dto.LoginDto;
import com.demo.service.UserService;
import com.demo.utils.ResponseResult;
import com.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/v1/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("/in")
    public ResponseResult login(@RequestBody LoginDto loginDto, HttpSession session) {
        session.removeAttribute("user");
        ResponseResult<User> result = userService.login(loginDto);
        if (result.getCode()==0){
            User user = result.getData();
            user.setPassword("");
            session.setAttribute("user",user);
        }
        return result;

    }

    @GetMapping("/{mobile}")
    public ResponseResult findRoleByName(@PathVariable("mobile") String name) {
        return userService.findRoleByName(name);

    }


}
